
#pragma  once

#include <stdint.h>
#include <windows.h>

namespace FlameEngine
{
	/**
	*A high resolution timer class
	*/
	class StopWatch
	{
	public:
		StopWatch():m_stoppedTime(0),m_stopped(true),m_startTime(0)
		{
			int64_t countsPerSec;
			QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
			m_secPerCount = 1.0f / countsPerSec;
		}

		//start timer
		void Start()
		{
			if(m_stopped)
			{
				QueryPerformanceCounter((LARGE_INTEGER*)&m_startTime);
				m_stopped = false;
			}
		}

		//stop timer
		void Stop()
		{
			if(!m_stopped)
			{
				QueryPerformanceCounter((LARGE_INTEGER*)&m_stoppedTime);
				m_stopped = true;
			}
		}
		
		//Get current elapsed time
		double GetElapsedTime()
		{
			if(m_stopped)
			{
				return (m_stoppedTime - m_startTime) * m_secPerCount;
			}
			else
			{
				int64_t curTime;
				QueryPerformanceCounter((LARGE_INTEGER*)&curTime);
				return (curTime - m_startTime) * m_secPerCount;
			}
		}

		//stop and reset timer
		void Reset()
		{
			m_startTime = 0;
			m_stoppedTime = 0;
			m_stopped = true;
		}

	private:
		int64_t m_stoppedTime;
		int64_t m_startTime;
		double m_secPerCount;
		bool m_stopped;
	};
}